<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: zhangyajun <448901948@qq.com>
// +----------------------------------------------------------------------

namespace page;

use think\Paginator;
use think\Request;
class ZanpianPage extends Paginator{
    protected function urls($page){
		//print_r(Request::instance());
		if(Request::instance()->module()=='admin' && Request::instance()->controller()!='Html'){
			return $this->url($page);
		}
		if(config('route')){
			$route=config('route');
		}else{
			$route=$GLOBALS['route'];
		}		
		$params=config('params');
        if ($page <= 0) {
            $page = 1;
        }
		if($page==1){
			unset($params['p']);
		}			
		$path=urldecode(zanpian_list_url($route,$params));
		$path = str_replace('[PAGE]', $page,$path);
        $url = $path;
        return $url;
    }
    public function getUrlRanges($start, $end)
    {
        $urls = [];

        for ($page = $start; $page <= $end; $page++) {
            $urls[$page] = $this->urls($page);
        }

        return $urls;
    }	
    protected function getTotal(){
        return '<li class="visible-xs active"><span class="num">'.$this->currentPage().'/'.$this->lastPage().'</span></li>';
    }
    /**
     * 上一页按钮
     * @param string $text
     * @return string
     */
    protected function getPreviousButton($text = "上一页")
    {

        if ($this->currentPage() <= 1) {
            return '<li class="disabled"><span>'.$text.'</span></li>';
        }

        $url = $this->urls(
            $this->currentPage() - 1
        );
		$page=$this->currentPage() - 1;
		
		
        return $this->getPageLinkWrapper($url, $page,$text);
    }

    /**
     * 下一页按钮
     * @param string $text
     * @return string
     */
    protected function getNextButton($text = '下一页')
    {
        if (!$this->hasMore) {
            return '<li class="disabled"><span>'.$text.'</span></li>';
        }

        $url = $this->urls($this->currentPage() + 1);
        $page=$this->currentPage() + 1;
        return $this->getPageLinkWrapper($url,$page,$text);
    }
    /**
     * 获取首页按钮
     */
    protected function getFirstButton($text = "首页"){
        if ($this->currentPage() <= 1) {
            return '<li class="disabled"><span>'.$text.'</span></li>';
        }

        $url = $this->urls(1);
        return $this->getPageLinkWrapper($url,1,$text);
    }
    /**
     * 获取尾页按钮
     */
    protected function getLastButton($text = "尾页"){
        if (!$this->hasMore) {
            return '<li class="disabled"><span>'.$text.'</span></li>';
        }
        $url = $this->urls($this->lastPage());
		$page=$this->lastPage();
        return $this->getPageLinkWrapper($url,$page,$text);
    }		
    /**
     * 页码按钮
     * @return string
     */
    protected function getLinks()
    {
        if ($this->simple)
            return '';

        $block = [
            'first'  => null,
            'slider' => null,
            'last'   => null
        ];

        $side   = 1;
        $window = $side * 2;
        if ($this->lastPage < $window + 4) {
            $block['first'] = $this->getUrlRanges(1, $this->lastPage);
        } elseif ($this->currentPage <= $window) {
            $block['first'] = $this->getUrlRanges(1, $window + 2);
            $block['last']  = $this->getUrlRanges($this->lastPage - 1, $this->lastPage);
        } elseif ($this->currentPage > ($this->lastPage - $window)) {
            $block['first'] = $this->getUrlRanges(1, 2);
            $block['last']  = $this->getUrlRanges($this->lastPage - ($window + 2), $this->lastPage);
        } else {
            $block['first']  = $this->getUrlRanges(1, 2);
            $block['slider'] = $this->getUrlRanges($this->currentPage - $side, $this->currentPage + $side);
            $block['last']   = $this->getUrlRanges($this->lastPage - 1, $this->lastPage);
        }

        $html = '';
        if (is_array($block['first'])) {
            $html .= $this->getUrlLinks($block['first']);
        }

        if (is_array($block['slider'])) {
            $html .= $this->getDots();
            $html .= $this->getUrlLinks($block['slider']);
        }

        if (is_array($block['last'])) {
            $html .= $this->getDots();
            $html .= $this->getUrlLinks($block['last']);
        }
        return $html;
    }

    /**
     * 渲染分页html
     * @return mixed
     */
    public function render()
    {
        if ($this->hasPages()) {
            if ($this->simple) {
                return sprintf(
                    '<ul class="pager">%s %s</ul>',
                    $this->getPreviousButton(),
                    $this->getNextButton()
                );
            } else {
                return sprintf(
                    '%s %s %s %s %s %s',
					$this->getFirstButton(),
                    $this->getPreviousButton(),
                    $this->getLinks(),
					$this->getTotal(),
                    $this->getNextButton(),
					$this->getLastButton()
                );
            }
        }
    }

    /**
     * 生成一个可点击的按钮
     *
     * @param  string $url
     * @param  int    $page
     * @return string
     */
    protected function getAvailablePageWrapper($url,$page,$txt)
    {	
        return '<li><a href="' . htmlentities($url) . '" data="p-'.$page.'">' . $txt . '</a></li>';
    }
    protected function getAvailablePageWrappers($url, $page)
    {	
        return '<li class="hidden-xs"><a href="' . htmlentities($url) . '" data="p-'.$page.'">' . $page . '</a></li>';
    }	

    /**
     * 生成一个禁用的按钮
     *
     * @param  string $text
     * @return string
     */
    protected function getDisabledTextWrapper($text)
    {
        return '<li class="disabled"><span>' . $text . '</span></li>';
    }

    /**
     * 生成一个激活的按钮
     *
     * @param  string $text
     * @return string
     */
    protected function getActivePageWrapper($text)
    {
        return '<li class="active"><span>' . $text . '</span></li>';
    }
    protected function getActivePageWrappers($text)
    {
        return '<li class="active hidden-xs"><span>' . $text . '</span></li>';
    }
    /**
     * 生成省略号按钮
     *
     * @return string
     */
    protected function getDots()
    {
        return '<li class="disabled hidden-xs"><span>...</span></li>';
    }

    /**
     * 批量生成页码按钮.
     *
     * @param  array $urls
     * @return string
     */
    protected function getUrlLinks(array $urls)
    {
        $html = '';

        foreach ($urls as $page => $url) {
            $html .= $this->getPageLinkWrappers($url, $page);
        }

        return $html;
    }

    /**
     * 生成普通页码按钮
     *
     * @param  string $url
     * @param  int    $page
     * @return string
     */
    protected function getPageLinkWrapper($url,$page,$txt)
    {
        if ($page == $this->currentPage()) {
            return $this->getActivePageWrapper($page);
        }
        return $this->getAvailablePageWrapper($url,$page,$txt);
    }
    protected function getPageLinkWrappers($url, $page)
    {

        if ($page == $this->currentPage()) {
            return $this->getActivePageWrappers($page);
        }

        return $this->getAvailablePageWrappers($url, $page);
    }	
}
